package edu.bit.water.service;


import edu.bit.water.dao.MetaAIDao;
import edu.bit.water.entity.ManagementIndicator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import java.util.*;

import edu.bit.water.entity.MetaAI;

/**
 * meta_assessment_indicator数据表对应的service类
 * @Author: Page
 */
@Service
public class MetaAIService {

    @Autowired
    private MetaAIDao metaAIDao;

    public List<MetaAI> selectMetaAI(String stNumber) {
        return metaAIDao.selectMetaAI(stNumber);
    }
    public List<MetaAI> selectMetaAINote(String stNumber) {
        return metaAIDao.selectMetaAINote(stNumber);
    }

    public Map<String, Object> insertMetaAI(MetaAI metaAI) {
        Map<String, Object> result = new HashMap<>();
        Integer id = metaAIDao.getMaxId();
        if (id != null) {
            metaAI.setId(id + 1);
        }
        int tmp = metaAIDao.insertMetaAI(metaAI);
        if (tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
    public Map<String, Object> insertMetaAINote(MetaAI metaAI) {
        Map<String, Object> result = new HashMap<>();
        Integer id = metaAIDao.getMaxId1();
        if (id != null) {
            metaAI.setId(id + 1);
        }
        int tmp = metaAIDao.insertMetaAINote(metaAI);
        if (tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    public Map<String, Object> deleteMetaAI(List<String> ids) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < ids.size(); i++) {
            int tmp = metaAIDao.deleteMetaAI(Integer.parseInt(ids.get(i)));
            if (tmp == 1) {
                result.put(ids.get(i).toString(), "success");
            }
            else {
                result.put(ids.get(i).toString(), "failed");
                flag = false;
            }
        }
        if (flag) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
    public Map<String, Object> deleteMetaAINote(List<String> ids) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < ids.size(); i++) {
            int tmp = metaAIDao.deleteMetaAINote(Integer.parseInt(ids.get(i)));
            if (tmp == 1) {
                result.put(ids.get(i).toString(), "success");
            }
            else {
                result.put(ids.get(i).toString(), "failed");
                flag = false;
            }
        }
        if (flag) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
    //级联删除
    public Map<String, Object> managementDelete(List<String> tempData) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;
        String standardNumber = tempData.get(tempData.size() -1);//最后一个数据是标准号
        for (int i = 0; i < tempData.size() - 1; i++) {
            int tmp = metaAIDao.managementDelete(tempData.get(i).toString(), standardNumber);
            if (tmp == 1) {
                result.put(tempData.get(i).toString(), "success");
            }
            else {
                result.put(tempData.get(i).toString(), "failed");
                flag = false;
            }
        }
        if (flag) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    public Map<String, Object> updateMetaAI(MetaAI metaAI) {
        Map<String, Object> result = new HashMap<>();
        int tmp = metaAIDao.updateMetaAI(metaAI);
        if(tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
    public Map<String, Object> updateMetaAINote(MetaAI metaAI) {
        Map<String, Object> result = new HashMap<>();
        int tmp = metaAIDao.updateMetaAINote(metaAI);
        if(tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
    //级联修改
    public Map<String, Object> managementUpdate(ManagementIndicator managementIndicator) {
        Map<String, Object> result = new HashMap<>();
        int tmp = metaAIDao.managementUpdate(managementIndicator);
        if(tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }
}
